package teleradiologia.repository;

import org.hibernate.Session;
import org.hibernate.Transaction;
import teleradiologia.domain.Customer;
import teleradiologia.utils.HibernateUtil;

public class CustomerDaoImpl implements CustomerDao {

	@Override
	public long create(Customer customer) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		long generatedId = (Long)session.save(customer);

		tx.commit();
		session.close();
		
		return ++generatedId;	
	}

	@Override
	public void delete(Customer customer) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(customer);

		tx.commit();
		session.close();
	}

	@Override
	public Customer getById(long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Customer customer = (Customer) session.get(Customer.class, new Long(id));
		
		tx.commit();
		session.close();

		return customer;
	}

	@Override
	public void update(Customer customer) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(customer);

		tx.commit();
		session.close();
	}
}
